/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.marks.editor;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import mtt.marks.events.ShowMarksViewEvent;
import mtt.rms.dataObjects.Mark;
import mtt.rms.events.recordstores.marks.events.StoreMarkEvent;
import mtt.rms.events.recordstores.marks.events.UpdateMarkEvent;

import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.TerminateEvent;
import hmvc.j2me.view.AView;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MarkEditorView extends AView {

	private Form form = new Form("Noten Editor");

	private TextField textFieldName;

	private TextField textFieldMark;

	private Command commandBack = new Command("zurück", Command.BACK, 1);

	private Command commandStore = new Command("Speichern", Command.OK, 1);
	
	/**
	 * contains the edited Mark, if not new
	 */
	private Mark mark;
	
	/**
	 * is it a new mark or editing an existing mark
	 */
	private boolean newMark = true;

	/**
	 * Creates an emtpy Editor
	 */
	public MarkEditorView() {
		initEmptyEditor();
	}

	/**
	 * Creats an Editor for the Mark
	 * 
	 * @param mark
	 */
	public MarkEditorView(Mark mark) {
		initEditor(mark);
	}

	public void commandAction(Command command, Displayable d) {
		if (command == commandBack) {
			getController().fireEventUp(new ShowMarksViewEvent());
			getController().fireEventLocal(new TerminateEvent());
		} else if (command == commandStore) {

			/*
			 * update an existing or store a new mark
			 */
			if (newMark) {
				getController().fireEventUp(new StoreMarkEvent(getMark()));
			} else {
				getController().fireEventUp(new UpdateMarkEvent(getMark()));
			}
		}
	}

	public void handleEvent(IEvent event) {
	}

	public void init() {

		this.form.addCommand(this.commandBack);
		this.form.addCommand(this.commandStore);

		setDisplayAble(this.form);
	}

	private void initEditor(Mark mark) {
		
		setMark(mark);
		newMark = false;

		this.textFieldName = new TextField("Name des Fachs: ", mark.getName(),
				30, TextField.ANY);
		this.textFieldMark = new TextField("Note / Punkte: ", mark.getMark(),
				3, TextField.DECIMAL);

		this.form.append(textFieldName);
		this.form.append(textFieldMark);
	}

	private void initEmptyEditor() {
		
		setMark(new Mark());

		this.textFieldName = new TextField("Name des Fachs: ", "", 30,
				TextField.ANY);
		this.textFieldMark = new TextField("Note / Punkte: ", "", 3,
				TextField.DECIMAL);

		this.form.append(textFieldName);
		this.form.append(textFieldMark);
	}

	public void registerEvents() {
	}

	public void terminate() {
		// TODO Auto-generated method stub

	}

	private Mark getMark() {
		
		mark.setName(textFieldName.getString());
		mark.setMark(textFieldMark.getString());
		
		return mark;
	}

	private void setMark(Mark mark) {
		this.mark = mark;
	}

}
